package org.zhaozhen.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhaozhen.basic.businessexception.BusinessException;
import org.zhaozhen.basic.service.impl.BaseServiceImpl;
import org.zhaozhen.org.domain.Employee;
import org.zhaozhen.org.domain.Shop;
import org.zhaozhen.org.mapper.EmployeeMapper;
import org.zhaozhen.org.mapper.ShopMapper;
import org.zhaozhen.org.service.IEmployeeService;
import org.zhaozhen.org.service.IShopService;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService{

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlement(Shop shop) {

        Employee admin = shop.getAdmin();
        //判断密码和确认密码是否重复
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码输入不一致，请重新输入");
        }
        //校验管理员是否已经注册过
        Employee employee = employeeMapper.loadByUser(admin.getUsername());
        if (employee!=null){
            throw new BusinessException("该用户已经注册过了");
        }
        //首先保存employee
        employeeService.add(admin);
        shop.setAdmin(admin);
        shopMapper.save(shop);
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
